//
//  NavigationControllerHelper.swift
//  iUtil
//
//  Created by wendachuan on 19/3/22.
//  Copyright © 2019年 wendachuan. All rights reserved.
//

import Foundation
import UIKit

public extension UINavigationBar {

    /// 设置导航栏返回按钮图标
    ///
    /// - parameter image: 返回按钮默认图片
    /// - parameter pressedImage: 返回按钮按下图片
    /// - parameter bundle: 图片所在的bundle
    class func setBackIndicatorImage(_ image: String, pressedImage: String, withHeight height: CGFloat? = nil, inBundle bundle: Bundle) {
        var defaultHeight: CGFloat = 26.0
        if height != nil {
            defaultHeight = height!
        }
        if let img = UIImage(named: image, in: bundle, compatibleWith: nil) {
            let image = UIImage(cgImage: img.cgImage!, scale: img.size.height * img.scale / defaultHeight, orientation: img.imageOrientation).withRenderingMode(UIImage.RenderingMode.alwaysOriginal)
            UINavigationBar.appearance().backIndicatorImage = image

            // hide title
            if #available(iOS 11.0, *) {
                UIBarButtonItem.appearance().setBackButtonTitlePositionAdjustment(UIOffset(horizontal: -200, vertical: 0), for: UIBarMetrics.default)
            } else {
                UIBarButtonItem.appearance().setBackButtonTitlePositionAdjustment(UIOffset(horizontal: CGFloat(Int.min), vertical: CGFloat(Int.min)), for: UIBarMetrics.default)
            }
        }

        if let pressedImg = UIImage(named: pressedImage, in: bundle, compatibleWith: nil) {
            UINavigationBar.appearance().backIndicatorTransitionMaskImage = UIImage(cgImage: pressedImg.cgImage!, scale: pressedImg.size.height * pressedImg.scale / defaultHeight, orientation: pressedImg.imageOrientation).withRenderingMode(UIImage.RenderingMode.alwaysOriginal)
        }
    }
    
    /// 设置底部线条
    ///
    /// - Parameters:
    ///   - color:
    ///   - height:
    func setBottomBorder(color: UIColor, height: CGFloat) {
        let bottomBorderRect = CGRect(x: 0, y: frame.height, width: frame.width, height: height)
        let bottomBorderView = UIView(frame: bottomBorderRect)
        bottomBorderView.backgroundColor = color
        addSubview(bottomBorderView)
    }
}
